#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h> 
#include "Serial.h"
#include "Motor.h"
#include "SmartCar.h"

// ??????
char USART_ReceiveString[50];
int Receive_sum = 0;
int Receive_Flag = 0;
int FSM_led;
int FSM_hc08;

// USART2 ?????
void USART2_Init(unsigned int Baudrate)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    // ?? TX ??
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // ?? RX ??
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // ?? USART2
    USART_InitStructure.USART_BaudRate = Baudrate;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART2, &USART_InitStructure);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    USART_Cmd(USART2, ENABLE);

    // ?? NVIC
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


// USART2 ??????
void USART2_IRQHandler(void)
{
    if (USART_GetITStatus(USART2, USART_IT_RXNE) == 1)//?? USART2 ???????????????????
    {
        if (Receive_sum > 49)//?????????? 49 ??,??????? '\0',???????,??????
        {
            USART_ReceiveString[49] = '\0';
            Receive_Flag = 1;
            Receive_sum = 0;
        }

        if (Receive_Flag == 0)//?????? 0,????????? USART_ReceiveString ????????
        {
            USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART2);
            Receive_sum++;
            if (USART_ReceiveString[Receive_sum - 1] == '%')//???? %,????????????
            {
                Receive_Flag = 1;
                Receive_sum = 0;
                printf("%s\r\n", USART_ReceiveString);
                // ??????????????
                if (strcmp(USART_ReceiveString, "channel:1%") == 0)
                {
                    Move_Forward();
                }
                else if (strcmp(USART_ReceiveString, "channel:2%") == 0)
                {
                    Move_Backward();
                }
                else if (strcmp(USART_ReceiveString, "left%") == 0)
                {
                    Turn_Left();
                }
                else if (strcmp(USART_ReceiveString, "right%") == 0)
                {
                    Turn_Right();
                }
                else if (strcmp(USART_ReceiveString, "stop%") == 0)
                {
                    Car_Stop();
                }
                else if (strcmp(USART_ReceiveString, "clockwise%") == 0)
                {
                    Clockwise_Rotation();
                }
                else if (strcmp(USART_ReceiveString, "counterclockwise%") == 0)
                {
                    CounterClockwise_Rotation();
                }
            }
        }

        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
}







